package com.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPInputStream extends InputStream {
    private DatagramSocket socket;
    private DatagramPacket packet;
    private byte[] buffer = new byte[1024];
    private int index = 0;
    private int length = 0;

    public UDPInputStream(DatagramSocket socket) {
        this.socket = socket;
        this.packet = new DatagramPacket(buffer, buffer.length);
    }

    @Override
    public int read() throws IOException {
        if (index >= length) {
            socket.receive(packet);
            buffer = packet.getData();
            length = packet.getLength();
            index = 0;
        }
        return buffer[index++] & 0xFF;
    }
}
